#include "BasicXml.h"
BasicXml::BasicXml()
{
    m_rebootIntervalTIme=0;
        m_rebootTimePoint=3;
}
bool BasicXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "PortList")
        {
            QDomNode portListChild = rootChild.firstChild();
            while(!portListChild.isNull())
            {
                if(portListChild.toElement().tagName() == "Port")
                {
                    PortStruct port;
                    QString type = portListChild.toElement().attribute("type");
                    port.setType(type);
                    QString name = portListChild.toElement().attribute("name");
                    port.setName(name);
                    QDomNode portChild = portListChild.firstChild();
                    while(!portChild.isNull())
                    {
                        if(portChild.toElement().tagName() == "PortName")
                        {
                            QString portName= portChild.toElement().text();
                            port.setPortName(portName);
                        }
                        if(portChild.toElement().tagName() == "Mode")
                        {
                            QString mode= portChild.toElement().text();
                            port.setMode(mode);
                        }
                        if(portChild.toElement().tagName() == "Baud")
                        {
                            QString baud = portChild.toElement().text();
                            port.setBaudRate(baud);
                        }
                        if(portChild.toElement().tagName() == "DataBit")
                        {
                            QString dataBit = portChild.toElement().text();
                            port.setDataBits(dataBit);
                        }
                        if(portChild.toElement().tagName() == "Parity")
                        {
                            QString parity = portChild.toElement().text();
                            port.setParity(parity);
                        }
                        if(portChild.toElement().tagName() == "StopBit")
                        {
                            QString stopBit = portChild.toElement().text();
                            port.setStopBits(stopBit);
                        }
                        if(portChild.toElement().tagName() == "FlowControl")
                        {
                            QString flowControl = portChild.toElement().text();
                            port.setFlowControl(flowControl);
                        }
                        if(portChild.toElement().tagName() == "EnablePin")
                        {
                            QString enablePin = portChild.toElement().text();
                            port.setEnablePinNumber(enablePin);
                        }
                        if(portChild.toElement().tagName() == "IP")
                        {
                            QString ip = portChild.toElement().text();
                            port.setIp(ip);
                        }
                        if(portChild.toElement().tagName() == "Port")
                        {
                            quint16 portNumber = portChild.toElement().text().toInt();
                            port.setPort(portNumber);
                        }
                        portChild = portChild.nextSibling();
                    }

                    bool isExist=false;
                    for(int i=0;i<m_portList.size();i++){
                        PortStruct p = m_portList.at(i);
                        if((p.type() == port.type())&&(p.name() == port.name()))
                        {
                            isExist=true;
                           break;
                        }
                    }
                    if(!isExist)
                    {
                        m_portList.append(port);
                    }



                }
                portListChild = portListChild.nextSibling();
            }
        }
        if(rootChild.toElement().tagName() == "DeviceList")
        {
            QDomNode deviceListChild = rootChild.firstChild();
            while(!deviceListChild.isNull())
            {
                if(deviceListChild.toElement().tagName() == "Device")
                {
                    DeviceStruct device;
                    QString type = deviceListChild.toElement().attribute("type");
                    device.setType(type);
                    QString name = deviceListChild.toElement().attribute("name");
                    device.setName(name);
                    QDomNode deviceChild = deviceListChild.firstChild();
                    while(!deviceChild.isNull())
                    {
                        if(deviceChild.toElement().tagName() == "BindingPort")
                        {
                            QStringList bindingPortList= deviceChild.toElement().text().split(",");
                            device.setBindingPortList(bindingPortList);
                        }
                        deviceChild = deviceChild.nextSibling();
                    }
                    m_deviceList.append(device);
                }
                deviceListChild = deviceListChild.nextSibling();
            }
        }
        if(rootChild.toElement().tagName() == "NotifyReboot")
              {
                  QDomNode rebootChild = rootChild.firstChild();

                  while(!rebootChild.isNull())
                  {


                      if (rebootChild.toElement().tagName() == "Interval")  //
                      {
                          bool ok;
                          QString timeinterval = rebootChild.toElement().text();

                          setRebootIntervalTime(timeinterval.toInt(&ok,10));
                      }
                      if (rebootChild.toElement().tagName() == "TimePoint")  //
                      {
                          bool ok;
                          QString timepoint = rebootChild.toElement().text();

                          setRebootTimePoint(timepoint.toInt(&ok,10));
                      }
                      rebootChild = rebootChild.nextSibling();


                   }
              }
        rootChild = rootChild.nextSibling();
    }
    file.close();
    return true;
}

QList<PortStruct> BasicXml::portList() const
{
    return m_portList;
}

void BasicXml::setPortList(const QList<PortStruct> &portList)
{
    m_portList = portList;
}

QList<DeviceStruct> BasicXml::deviceList() const
{
    return m_deviceList;
}

void BasicXml::setDeviceList(const QList<DeviceStruct> &deviceList)
{
    m_deviceList = deviceList;
}

bool BasicXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }

    m_portList.clear();
    return true;
}


quint32 BasicXml::rebootIntervalTIme()
{
    return m_rebootIntervalTIme;
}
void BasicXml::setRebootIntervalTime(quint32 t)
{
    m_rebootIntervalTIme = t;
}

quint8 BasicXml::rebootTimePoint()
{
    return m_rebootTimePoint;
}
void BasicXml::setRebootTimePoint(quint8 t)
{
    m_rebootTimePoint=t;
}
